<template>
    <div class="app-container">
        <el-row :gutter="10">
            <el-col v-if="isShowLeftPatientInfo" :span="8">
                <nurse-patient-bed-list ref="nursePatientBedListRef" :is-multiple="isMultiple"
                                        @patient-selection-change="patientSelectionChange"/>
            </el-col>
            <el-col :span="isShowLeftPatientInfo ? 16:24">
                <div class="search-container">
                    <el-form ref="showParamsRef" :inline="true" :model="queryParams" label-width="65px">
                        <el-row style="margin-top: -10px;margin-bottom: -15px;">
                            <el-form-item label="长/临:" prop="repeatIncaditor">
                                <el-radio-group v-model="queryParams.repeatIncaditor" @change="handleQuery">
                                    <template v-for="(tag, index) in dictDataMap['order_type']">
                                        <el-radio-button :label="tag.dictLabel" :value="tag.dictValue"/>
                                    </template>
                                </el-radio-group>
                            </el-form-item>
                            <el-form-item>
                                <query-form-button :is-show="false" @handleQuery="handleQuery"/>
                                <el-button type="success" @click="orderListPrint">
                                    <template #icon>
                                        <i-ep-printer/>
                                    </template>
                                    打印
                                </el-button>
                            </el-form-item>
                        </el-row>
                    </el-form>
                </div>
                <el-table v-adaptive="290" :border="true" :data="orderList" :header-cell-style="$headerCellStyle"
                          :stripe="true" highlight-current-row @select-all="selectAll">
                    <el-table-column :align="'center'" type="selection" width="40">
                        <template #default="scope">
                            <el-checkbox v-model="scope.row.checkOk"
                                         @change="checkboxChange($event, scope.row)"/>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" fixed="left" label="下达时间" prop="createTime" width="140">
                        <template #default="scope">
                            {{ $parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'left'" :header-align="'center'" :show-overflow-tooltip="true" fixed="left"
                                     label="医嘱内容"
                                     prop="orderText">
                        <template #default="scope">
                    <span
                        style="color: #000000;font-size: large;">{{
                            scope.row.groupNumberStyle != '' ? scope.row.groupNumberStyle : '&nbsp;&nbsp;&nbsp;'
                        }}</span>
                            {{ scope.row.orderText }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="途径" prop="channel" width="100">
                        <template #default="scope">
                            {{ scope.row.channelName }}
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="频次" prop="performFreq" width="100">
                        <template #default="scope">
                            {{ scope.row.performFreqName }}
                        </template>
                    </el-table-column>
                    <!--                    <el-table-column :align="'left'" :header-align="'center'" :show-overflow-tooltip="true"-->
                    <!--                                     label="执行时间"-->
                    <!--                                     prop="nursePerformTime" width="100">-->
                    <!--                        <template #default="scope">-->
                    <!--                            {{ scope.row.nursePerformTime }}-->
                    <!--                        </template>-->
                    <!--                    </el-table-column>-->
                    <el-table-column :align="'center'" label="单次剂量" prop="singleDosage" width="100">
                        <template #default="scope">
                    <span style="margin-left: -15px"> {{ scope.row.singleDosage }}<dict-data-tag
                        :options="dictDataMap['dose_unit']" :value="scope.row.doseUnits"
                        style="margin: -23px -20px 0px 35px"/></span>
                        </template>
                    </el-table-column>
                    <el-table-column :align="'center'" label="执行护士" prop="executeNurseName" width="90"/>
                    <el-table-column :align="'center'" label="医生停止时间" prop="stopInTime" width="140"/>
                    <el-table-column :align="'center'" label="护士停止时间" prop="nurseStopTime" width="140"/>
                    <el-table-column :align="'center'" label="停止护士" prop="stopNurseName" width="90"/>
                </el-table>
            </el-col>
        </el-row>
    </div>
</template>

<script lang="ts" setup>

import {getOrderListPrint} from "@/api/orders/orders.ts"
import {useUserStoreHook} from "@/store/modules/user.ts";
import {useDictStoreHook} from "@/store/modules/dict.ts";

const {proxy} = getCurrentInstance() as any;
const currentSelectDept = proxy.$currentSelectDept
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('sys_user_sex', 'dose_unit', 'order_type'))
const orderList = ref<any>([])
// orders id 集合
const ids = ref<any>([])
const objectUrl = ref("")
// 床位是否多选
const isMultiple = ref<boolean>(true)
const nursePatientBedListRef = ref()
const isShowLeftPatientInfo = ref<boolean>(true)
const queryParams = ref<any>({
    repeatIncaditor: '0',
    visitIdArray: [],
    visitId: ''
})


/**
 * 医嘱明细全选事件 全选/反选
 */
function selectAll() {
    if (ids.value.length === orderList.value.length) {
        orderList.value.forEach((order: any) => {
            order.checkOk = false
        })
        ids.value = []
    } else {
        orderList.value.forEach((order: any) => {
            order.checkOk = true
        })
        ids.value = orderList.value.map((item: any) => item.detailId);
    }
}

// 行选中 默认按照组号相同的勾选或者取消勾选
function checkboxChange(e: boolean, detail: any) {
    if (e) {
        orderList.value.forEach((order: any) => {
            if (detail.groupNumber != null && order.groupNumber === detail.groupNumber && order.visitId == detail.visitId) {
                order.checkOk = true
            }
        })
    } else {
        orderList.value.forEach((order: any) => {
            if (detail.groupNumber != null && order.groupNumber === detail.groupNumber && order.visitId == detail.visitId) {
                order.checkOk = false
            }
        })
    }
}


/**
 * 患者床位列表勾选事件
 */
function patientSelectionChange(visitIds: any) {
    queryParams.value.visitIdArray = visitIds
    orderListSelect()

}

function handleQuery() {
    orderListSelect()
}

/**
 * 查询医嘱
 */
function orderListSelect() {
    getOrderListPrint(queryParams.value).then((res: resType) => {
        orderList.value = res.data
        ids.value = orderList.value
        selectAll()
    })
}

/**
 * 父页面点击跳转事件
 * @param isShowLeftPatientInfoThis
 * @param visitIdThis
 * @param isMultipleThis 是否多选 Y是，N否
 */
function initVisitOrderPrint(isShowLeftPatientInfoThis: boolean, visitIdThis: string, isMultipleThis: boolean) {
    isShowLeftPatientInfo.value = isShowLeftPatientInfoThis
    if (!isShowLeftPatientInfoThis) {
        queryParams.value.visitId = visitIdThis
        orderListSelect()
    } else {
        isMultiple.value = isMultipleThis
        nextTick(() => {
            nursePatientBedListRef.value.handleQuery()
        })
    }
}


/**
 * 最终医嘱打印
 */
function orderListPrint() {
    const checkList = orderList.value.filter((item: any) => item.checkOk)
    if (checkList.length <= 0) {
        return ElMessage.error("请勾选需要打印的医嘱")
    }
    let ids = ""
    checkList.forEach((item: any) => {
        ids += item.id + ","
    })
    let url = "/business/orders/orderListPrint";
    const formData = new FormData()
    formData.append("repeatIncaditor", queryParams.value.repeatIncaditor)
    formData.append("visitId", queryParams.value.visitId)
    formData.append("ordersIdListStr", ids)
    formData.append("orgId", userInfo.orgId)
    formData.append("longTemplateCode", "longOrdersMultipleTogetherPrint")
    formData.append("shortTemplateCode", "shortOrdersMultipleTogetherPrint")
    // pdf打印 、打印预览
    proxy.$printPdfPublic(url, formData).then(res => {
        if (res) {
            objectUrl.value = URL.createObjectURL(res)
        } else {
            console.error('打印失败')
        }
    })
}

defineExpose({
    initVisitOrderPrint
})
</script>


<style scoped>

</style>
